const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const webpack = require('webpack');
const LodashModuleReplacementPlugin = require('lodash-webpack-plugin');

module.exports = {
    plugins: [
        new LodashModuleReplacementPlugin(),
        new CleanWebpackPlugin(),
        new MiniCssExtractPlugin({
            filename: 'css/[name].css?[hash]'
        }),
        new webpack.DefinePlugin({
            "env": JSON.stringify(process.env.NODE_ENV)
        })
    ],
    // 压缩文件
    optimization: {
        splitChunks: {
            chunks: 'all',
            automaticNameDelimiter: '~',
            name: true,
            cacheGroups: {
                default: {
                    name: 'common',
                    chunks: 'all',
                    minChunks: 3,
                    priority: -5
                },
                echarts: {
                    test: /[\\/]node_modules[\\/](echarts)/,
                    priority: -8,
                    name: 'echarts'
                },
                vendor: {
                    // node_modules内的依赖库
                    chunks: 'all',
                    test: /[\\/]node_modules[\\/](?!(echarts))/,
                    reuseExistingChunk: true,
                    minSize: 1000,
                    name: true,
                    minChunks: 1,
                    maxInitialRequests: 5,
                    priority: -21, // 该配置项是设置处理的优先级，数值越大越优先处理
                }
            },
        },
        runtimeChunk: {
            name: 'manifest',
        },
    },
    module: {
        rules: [
            {
                test: /\.css$/,
                use: [MiniCssExtractPlugin.loader, 'css-loader'],
            },
            {
                test: /\.scss$/,
                use: [
                    {
                        loader: MiniCssExtractPlugin.loader,
                        options: {
                            publicPath: '../'
                        }
                    },
                    {
                        loader: "css-loader",
                        options: {
                            modules: {
                                localIdentName: '[name]__[local]--[hash:base64:5]'
                            }
                        }
                    },
                    {
                        loader: 'sass-loader',
                        options: {
                            sourceMap: true
                        },
                    }
                    ],
            },
            {
                test: /\.(jsx|js)$/,
                exclude: /(node_modules)/,
                use: {
                    loader: 'babel-loader',
                    options: {
                        plugins: [
                            [
                                'transform-runtime',
                            ]
                        ]
                    }
                }
            },
            {
                test: /\.(png|svg|jpg|gif)$/,
                use: [
                    {
                        loader: 'file-loader'
                    }
                ],
            },
            {
                test: /\.(woff|woff2|eot|ttf|otf|svg)$/,
                use: [
                    {
                        loader: 'file-loader',
                        options: {
                            outputPath: 'iconfont/'
                        }
                    }
                ]
            },
            {
                test: /\.html$/,
                use: [ {
                    loader: 'html-loader',
                    options: {
                        minimize: true
                    }
                }],
            }
        ]
    }
};
